package gw.com.android.upgrade;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.gwtsz.gts2.hx.R;

import java.io.File;

import butterknife.ButterKnife;
import gw.com.android.app.GTConfig;
import www.com.library.app.AppActivities;
import www.com.library.app.Logger;
import www.com.library.app.ObjectSessionStore;
import www.com.library.view.TintTextView;

/**
 * 升级提示
 * 
 * @author reeta
 * 
 */
public class UpdateDialog extends Dialog implements View.OnClickListener {

	/** 進度條 */
	ProgressBar mProgressBar;
	/**升級內容提示*/
	TextView mContentView;
	/**升级进度条控件*/
	View mDownloadView;
	/**升级进度提醒*/
	TextView mPerView;
	TextView mCurSizeView;
	private String url;
	private Handler handler;
	private String mContentText;
	private boolean mHasNegButton = true;
	private Context context = null;
	protected static Dialog mInstance = null;
	private TintTextView mBtnPos;
	private TintTextView mBtnNeg;
	private View app_version_update_view;

	public static Dialog  instance(){
		return mInstance;
	}

	public UpdateDialog(Context context, String contentText, String url, Handler handler, boolean isForce) {
		super(context, R.style.dialog_loading_bar_no_frame);
		mInstance = this;
		this.context = context;
		this.url = url;
		this.handler = handler;
		mContentText = contentText;
		if(isForce){
			mHasNegButton = false;
			setCancelable(false);
		}
		initParam();
	}


	public void initParam() {LayoutInflater inflater = getLayoutInflater();
		if (inflater != null) {
			View rootView = inflater.inflate(R.layout.dialog_upgrade_action, null, false);
			inflaterCustomView(rootView);

			ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
			/* The next line will add the ProgressBar to the dialog. */
			addContentView(rootView, lp);
		}

//		mCustomViewResId = R.layout.dialog_upgrade_action;
	}


	public void inflaterCustomView(View mCustomView) {
		ButterKnife.bind(mCustomView);

//		if(!mHasNegButton){
//			mBtnPos.setBackgroundResource(R.drawable.dialog_btn_bg_selector);
//		}
		mBtnPos = (TintTextView) mCustomView.findViewById(R.id.app_upgrade_update);
		mBtnNeg = (TintTextView) mCustomView.findViewById(R.id.app_upgrade_cancel);
		mProgressBar = (ProgressBar) mCustomView.findViewById(R.id.download_progressbar);
		mContentView = (TextView) mCustomView.findViewById(R.id.tv_content);
		mDownloadView = mCustomView.findViewById(R.id.ll_download);
		mPerView  = (TextView) mCustomView.findViewById(R.id.tv_percentage);
		mCurSizeView  = (TextView) mCustomView.findViewById(R.id.tv_size);
		mContentView.setText(mContentText);
		app_version_update_view = mCustomView.findViewById(R.id.app_version_update_view);
		mBtnPos.setOnClickListener(this);
		mBtnNeg.setOnClickListener(this);
		if (mHasNegButton) {
			mBtnNeg.setClickable(true);
			mBtnNeg.setEnabled(true);
			mBtnNeg.setOnClickListener(this);
		} else {
			mBtnNeg.setVisibility(View.GONE);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.app_upgrade_cancel:
			dismiss();
			break;
		case R.id.app_upgrade_update:
			mDownloadView.setVisibility(View.VISIBLE);
			app_version_update_view.setVisibility(View.GONE);
			setCancelable(false);
			checkIntent(url, handler);
			break;
		}
	}

	/**
	 * 确定升级时的检查,进行apk下载
	 *
	 */
	public void checkIntent(String url, Handler handler) {
		try {
			// by reeta
			// 经测试发现，如果保存下载包的目录不存在的话，获取到的写入流一直为null，导致下载失败，所以此处加入判断，如果不存在先创建目录
			// 根据网上资料表示，在2.2之后，FileOutputStream就不再支持文件不存在就自动帮你创建的机制了.
			File file = new File(GTConfig.instance().getRootDirectory());
			if (!file.exists()) {
				file.mkdirs();
			}

			intentToUpgradeService(url, handler);

		} catch (Exception e) {
			Logger.e(e);
			intentToUpgradeService(url, handler);
		}
	}

	/**
	 * 跳转到下载app的service
	 */
	private void intentToUpgradeService(String url, Handler handler) {
		Intent intent = new Intent();
		Bundle bundle = new Bundle();
		bundle.putString("url", url);
		bundle.putString("upgradeHandler", ObjectSessionStore.insertObject(handler));
		intent.putExtras(bundle);
		intent.setClass(AppActivities.getSingleton().currentActivity(), AppUpgradeService.class);
		AppActivities.getSingleton().currentActivity().startService(intent);
	}
}